package com.zhouwj.utils.ViewUtils;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.util.Log;

import java.util.Calendar;

/**
 * Created by Administrator on 2016/12/7.
 */

public class DateTimePickerDialog {
    private final String TAG = DateTimePickerDialog.class.getSimpleName();
    private volatile static DateTimePickerDialog mInstance;
    private DatePickerDialog.OnDateSetListener mOnDateSetListener;
    private TimePickerDialog.OnTimeSetListener mOnTimeSetListener;
    private DatePickerDialog mDatePickerDialog;
    private TimePickerDialog mTimePickerDialog;
    private Calendar mCalendar;

    public static DateTimePickerDialog getInstance(){
        if (mInstance == null) {
            synchronized (DateTimePickerDialog.class) {
                if (mInstance == null) {
                    mInstance = new DateTimePickerDialog();
                }
            }
        }
        return mInstance;
    }

    public void setOnDateSetListener(DatePickerDialog.OnDateSetListener listener){
        mOnDateSetListener = listener;
    }

    public void setOnTimeSetListener(TimePickerDialog.OnTimeSetListener listener) {
        mOnTimeSetListener = listener;
    }

    public void showDatePickerDialog(Context context){
        if(mOnDateSetListener==null){
            Log.e(TAG,"Please setOnDateSetListener");
            return;
        }
        mCalendar = Calendar.getInstance();
        if(mDatePickerDialog==null){
            mDatePickerDialog = new DatePickerDialog(context,mOnDateSetListener,mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH));
        }
        mDatePickerDialog.show();
    }

    public void showTimePickerDialog(Context context){
        if(mOnTimeSetListener==null){
            Log.e(TAG,"Please setOnTimeSetListener");
            return;
        }
        mCalendar = Calendar.getInstance();
        if(mTimePickerDialog==null){
            mTimePickerDialog = new TimePickerDialog(context,mOnTimeSetListener,mCalendar.get(Calendar.HOUR_OF_DAY),mCalendar.get(Calendar.MINUTE),true);
        }
        mTimePickerDialog.show();
    }
}
